package Qimo;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class BookText {
    static Scanner sc=new Scanner(System.in);
    static ArrayList<Book> list=new ArrayList<>();
    static {
        list.add(new Book(1,"三国演义",58.00,100));
        list.add(new Book(2,"西游记",26.00,20));
        list.add(new Book(3,"红楼梦",55.00,40));
        list.add(new Book(4,"水浒传",100.00,80));
        list.add(new Book(5,"猪猪侠",34.00,150));
    }
    public static void main(String[] args) throws IOException {
        while (true){
            System.out.println("请输入编号：1.写入文件 2.读取文件 3.删除 4.同步文件");
            int n=sc.nextInt();
            switch (n){
                case 1:
                    asdf1();
                    break;
                case 2:
                    asdf2();
                    break;
                case 3:
                    asdf3();
                    break;
                case 4:
                    asdf4();
                    break;
            }
        }
    }

    private static void asdf4() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("book.txt"));
        for (Book book : list) {
            bw.write(book.toString());
            bw.newLine();
        }
        bw.close();
    }

    private static void asdf3() {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getFlag()>=1){
                System.out.println("价格是"+list.get(i).getPrice()*0.9);
            } else if (list.get(i).getFlag()==0) {
                list.remove(i);
            }
        }
    }

    private static void asdf2() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("book.txt"));
        String str;
        while ((str=br.readLine())!=null){
        }
        for (Book book : list) {
            System.out.println(book);
        }
        br.close();
    }

    private static void asdf1() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("book.txt"));
        for (Book book : list) {
            bw.write(book.toString());
            bw.newLine();
        }
        bw.close();
    }
}
